package net.hn.hnms.biz.emgcy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.hn.hnms.biz.emgcy.domain.MaterialPo;
import net.hn.hnms.biz.emgcy.domain.dto.MaterialDto;
import net.hn.hnms.biz.emgcy.domain.vo.MaterialVo;
import net.hn.hnms.biz.emgcy.mapper.MaterialMapper;
import net.hn.hnms.biz.emgcy.service.MaterialSrvice;
import net.hn.hnms.biz.emgcy.util.FileUtil;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.system.api.RemoteDictService;
import net.hn.hnms.sys.system.api.RemoteUserService;
import net.hn.hnms.sys.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author: baijiajun
 * @Date: 2022/9/13
 */
@Service
public class MaterialSrviceImpl extends ServiceImpl<MaterialMapper, MaterialPo> implements MaterialSrvice {
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private RemoteDictService remoteDictService;
    @Autowired
    private RemoteUserService remoteUserService;

    @Override
    public ResponsePage<MaterialVo> pageList(PageQuery pageQuery, MaterialDto materialDto) {
        pageQuery.setPageNum(pageQuery.getPageNum() >= 1 ? (pageQuery.getPageNum() - 1) * pageQuery.getPageSize() : 0);
        List<MaterialVo> list = materialMapper.pageList(pageQuery.getPageSize(), pageQuery.getPageNum(), materialDto);
        if (list.isEmpty()) {
            return new ResponsePage<>(new ArrayList(), 0);
        }
        list.forEach(s -> {
            if (StringUtils.isNotEmpty(s.getType())) {
                String[] split = s.getType().split(",");
                s.setCode(split);
            }
            if (s.getUserId() != null) {
                LoginUser userInfoById = remoteUserService.getUserInfoById(s.getUserId());
                s.setCreateUserName(userInfoById.getNickname());
            }
        });
        return new ResponsePage<>(list, list.get(0).getTotal());
    }

    @Override
    public Boolean add(MaterialPo materialPo) {
        int insert = materialMapper.insert(materialPo);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean update(MaterialPo materialPo) {
        int update = materialMapper.updateById(materialPo);
        if (update > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean delete(Long id) {
        int delete = materialMapper.deleteById(id);
        if (delete > 0) {
            return true;
        }
        return false;
    }

    @Override
    public void export(HttpServletResponse response) throws IOException {
        List<MaterialPo> list = materialMapper.selectList(null);
        List<Map<String, Object>> listExport = new ArrayList<>();
        list.forEach(item -> {
            Map<String, Object> map = new LinkedHashMap<>();
            String[] split = item.getType().split(",");
            String str = "";
            for (String s : split) {
                if (StringUtils.isNotEmpty(s)) {
                    String dict = remoteDictService.selectDictLabel("material_type", s);
                    if (StringUtils.isNotEmpty(dict)) {
                        str += dict + "/";
                        map.put("资源种类", str.substring(0, str.length() - 1));
                    } else {
                        map.put("资源种类", null);
                    }
                } else {
                    map.put("资源种类", null);
                }
            }
            map.put("资源名称", item.getName());
            map.put("规格", item.getSpecification());
            map.put("数量", item.getQuantity());
            map.put("计量单位", item.getUnit());
            map.put("资源存放地点", item.getAddress());
            map.put("责任单位", item.getDept());
            map.put("责任人", item.getKeeper());
            map.put("联系电话", item.getTel());
            map.put("备注", item.getDescription());

           /* map.put("购入时间", item.getTobuyAt());
            map.put("物资出厂日期",item.getProductAt());
            map.put("物资有效截止日期",item.getValidityAt());

            map.put("物资性能说明",item.getPerformance());*/

            listExport.add(map);
        });
        FileUtil.downloadExcel(listExport, response);
    }
}
